package edu.csumb.itcd.teammario;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;



public class ShopMenu extends JPanel implements ActionListener{

	private JButton backButton;
	private JTextField moneyField;
	
	private JButton healthButton;
	private JTextField healthField;
	
	private JButton weaponRegDmgButton;
	private JButton weaponRegRofButton;
	private JButton weaponPoisonBuyButton;
	private JButton weaponPoisonDmgButton;
	private JButton weaponPoisonDurButton;
	private JButton weaponShotgunBuyButton;
	private JButton weaponShotgunDmgButton;
	private JButton weaponShotgunRofButton;
	private JLabel weaponRegDmgCostLabel;
	private JTextField weaponRegDmgField;
	private JLabel weaponRegRofCostLabel;
	private JTextField weaponRegRofField;
	private JLabel weaponPoisonBuyCostLabel;
	private JLabel weaponPoisonDmgCostLabel;
	private JTextField weaponPoisonDmgField;
	private JLabel weaponPoisonDurCostLabel;
	private JTextField weaponPoisonDurField;
	private JLabel weaponShotgunBuyCostLabel;
	private JLabel weaponShotgunDmgCostLabel;
	private JTextField weaponShotgunDmgField;
	private JLabel weaponShotgunRofCostLabel;
	private JTextField weaponShotgunRofField;
	
	private JButton towerRegDmgButton;
	private JButton towerRegRofButton;
	private JButton towerPoisonDmgButton;
	private JButton towerPoisonDurButton;
	private JButton towerShotgunDmgButton;
	private JButton towerShotgunRofButton;
	private JTextField towerRegDmgField;
	private JTextField towerRegRofField;
	private JTextField towerPoisonDmgField;
	private JTextField towerPoisonDurField;
	private JTextField towerShotgunDmgField;
	private JTextField towerShotgunRofField;
	
	private int playerMoney = Player.getMoney();
	private int playerHealth = Player.getMaxHealth();
	
	private int weaponRegDmg = Player.getWeaponByIndex(0).getWeaponDamage();
	private int weaponRegRof = Player.getWeaponByIndex(0).getRateOfFire();
	private int weaponPoisonDmg;
	private int weaponPoisonDur;
	private int weaponShotgunDmg;
	private int weaponShotgunRof;
	
	private int towerRegDmg = Tower.getDamage();
	private int towerRegRof = Tower.getRateOfFire();
	private int towerPoisonDmg = PoisonTower.getDamage();
	private int towerPoisonDur = PoisonTower.getDuration();
	private int towerShotgunDmg = ShotgunTower.getDamage();
	private int towerShotgunRof = ShotgunTower.getRateOfFire();
	
	private int playerHealthCost = 100;
	
	private int weaponRegDmgCost = 50;
	private int weaponRegRofCost = 50;
	private int weaponPoisonCost = 300;
	private int weaponPoisonDmgCost = 60;
	private int weaponPoisonDurCost = 100;
	private int weaponShotgunCost = 400;
	private int weaponShotgunDmgCost = 70;
	private int weaponShotgunRofCost = 150;
	
	private int towerRegDmgCost = 100;
	private int towerRegRofCost = 100;
	private int towerPoisonDmgCost = 120;
	private int towerPoisonDurCost = 200;
	private int towerShotgunDmgCost = 140;
	private int towerShotgunRofCost = 300;
	
	private boolean change = false;
	private boolean enteredShop = true;
	private boolean boughtPoisonWeapon = false;
	private boolean boughtShotgunWeapon = false;
	
	private JButton undoButton;
	private Font subHeadFont = new Font("Comic Sans MS", Font.BOLD, 18);
	
	public ShopMenu()
	{
		setLayout(new BorderLayout());
		
		//Title Section
		JPanel headerPanel = new JPanel();
		headerPanel.setLayout(new GridLayout(1, 3));
		add(headerPanel, BorderLayout.NORTH);
		
		JPanel backButtonPanel = new JPanel();
		backButtonPanel.setLayout(new FlowLayout());
		headerPanel.add(backButtonPanel);
		
		backButton = new JButton("Back");
		backButton.addActionListener(this);
		backButtonPanel.add(backButton);
		
		JPanel shopLabelPanel = new JPanel();
		shopLabelPanel.setLayout(new FlowLayout());
		headerPanel.add(shopLabelPanel);
		
		JLabel shopLabel = new JLabel("Shop");
		shopLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 24));
		shopLabelPanel.add(shopLabel);
		
		JPanel moneyPanel = new JPanel();
		moneyPanel.setLayout(new FlowLayout());
		headerPanel.add(moneyPanel);
		
		JLabel moneyLabel = new JLabel("Money:");
		moneyPanel.add(moneyLabel);
		
		moneyField = new JTextField(5);
		moneyField.setEditable(false);
		moneyField.setBackground(Color.white);
		moneyField.setText("$" + Player.getMoney());
		moneyPanel.add(moneyField);
		
		undoButton = new JButton("Undo Changes");
		undoButton.addActionListener(this);
		moneyPanel.add(undoButton);
		
		//Player Section
		//Player Label Section
		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new GridLayout(3, 1));
		add(contentPanel, BorderLayout.CENTER);
		
		JPanel playerPanel = new JPanel();
		playerPanel.setLayout(new BorderLayout());
		playerPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		contentPanel.add(playerPanel);
		
		JPanel playerLabelPanel = new JPanel();
		playerLabelPanel.setLayout(new FlowLayout());
		playerPanel.add(playerLabelPanel, BorderLayout.NORTH);
		
		JLabel playerLabel = new JLabel("Player");
		playerLabel.setFont(subHeadFont);
		playerLabelPanel.add(playerLabel);
		
		//Player Button Section
		JPanel playerUpgradePanel = new JPanel();
		playerUpgradePanel.setLayout(new GridLayout(2, 1));
		playerPanel.add(playerUpgradePanel);
		
		JPanel healthButtonPanel = new JPanel();
		healthButtonPanel.setLayout(new FlowLayout());
		playerUpgradePanel.add(healthButtonPanel);
		
		healthButton = new JButton("HEALTH+10");
		healthButton.addActionListener(new PlayerButtonListener());
		healthButtonPanel.add(healthButton);
		
		//Player Cost Section
		JPanel healthCostPanel = new JPanel();
		healthCostPanel.setLayout(new FlowLayout());
		playerUpgradePanel.add(healthCostPanel);
		
		JLabel healthCostLabel = new JLabel("$" + playerHealthCost);
		healthCostPanel.add(healthCostLabel);
		
		healthField = new JTextField(5);
		healthField.setText(playerHealth + " HP");
		healthField.setEditable(false);
		healthField.setBackground(Color.white);
		healthCostPanel.add(healthField);
		
		//Weapon Section
		//Weapon Label Section
		JPanel weaponPanel = new JPanel();
		weaponPanel.setLayout(new BorderLayout());
		weaponPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		contentPanel.add(weaponPanel);
		
		JPanel weaponLabelPanel = new JPanel();
		weaponLabelPanel.setLayout(new FlowLayout());
		weaponPanel.add(weaponLabelPanel, BorderLayout.NORTH);
		
		JLabel weaponLabel = new JLabel("Weapon");
		weaponLabel.setFont(subHeadFont);
		weaponLabelPanel.add(weaponLabel);
		
		JPanel weaponUpgradePanel = new JPanel();
		weaponUpgradePanel.setLayout(new GridLayout(3, 3));
		weaponPanel.add(weaponUpgradePanel, BorderLayout.CENTER);
		
		JPanel weaponRegUpgradeLabelPanel = new JPanel();
		weaponRegUpgradeLabelPanel.setLayout(new FlowLayout());
		weaponUpgradePanel.add(weaponRegUpgradeLabelPanel);
		
		JLabel weaponRegLabel = new JLabel("Regular");
		weaponRegUpgradeLabelPanel.add(weaponRegLabel);
		
		JPanel weaponPoisonUpgradeLabelPanel = new JPanel();
		weaponPoisonUpgradeLabelPanel.setLayout(new FlowLayout());
		weaponUpgradePanel.add(weaponPoisonUpgradeLabelPanel);
		
		JLabel weaponPoisonLabel = new JLabel("Poison");
		weaponPoisonUpgradeLabelPanel.add(weaponPoisonLabel);
		
		JPanel weaponShotgunUpgradeLabelPanel = new JPanel();
		weaponShotgunUpgradeLabelPanel.setLayout(new FlowLayout());
		weaponUpgradePanel.add(weaponShotgunUpgradeLabelPanel);
		
		JLabel weaponShotgunLabel = new JLabel("Shotgun");
		weaponShotgunUpgradeLabelPanel.add(weaponShotgunLabel);
		
		//Weapon Button Section
		JPanel weaponRegButtonPanel = new JPanel();
		weaponRegButtonPanel.setLayout(new FlowLayout());
		weaponUpgradePanel.add(weaponRegButtonPanel);
		
		weaponRegDmgButton = new JButton("DMG+1");
		weaponRegDmgButton.addActionListener(new WeaponButtonListener());
		weaponRegButtonPanel.add(weaponRegDmgButton);
		
		weaponRegRofButton = new JButton("RoF-1");
		weaponRegRofButton.addActionListener(new WeaponButtonListener());
		weaponRegButtonPanel.add(weaponRegRofButton);
		
		JPanel weaponPoisonButtonPanel = new JPanel();
		weaponPoisonButtonPanel.setLayout(new FlowLayout());
		weaponUpgradePanel.add(weaponPoisonButtonPanel);
		
		weaponPoisonBuyButton = new JButton("Buy");
		weaponPoisonBuyButton.addActionListener(new WeaponButtonListener());
		weaponPoisonButtonPanel.add(weaponPoisonBuyButton);
		
		weaponPoisonDmgButton = new JButton("DMG+1");
		weaponPoisonDmgButton.addActionListener(new WeaponButtonListener());
		weaponPoisonButtonPanel.add(weaponPoisonDmgButton);
		weaponPoisonDmgButton.setVisible(false);
		
		weaponPoisonDurButton = new JButton("DUR+1");
		weaponPoisonDurButton.addActionListener(new WeaponButtonListener());
		weaponPoisonButtonPanel.add(weaponPoisonDurButton);
		weaponPoisonDurButton.setVisible(false);
		
		JPanel weaponShotgunButtonPanel = new JPanel();
		weaponShotgunButtonPanel.setLayout(new FlowLayout());
		weaponUpgradePanel.add(weaponShotgunButtonPanel);
		
		weaponShotgunBuyButton = new JButton("Buy");
		weaponShotgunBuyButton.addActionListener(new WeaponButtonListener());
		weaponShotgunButtonPanel.add(weaponShotgunBuyButton);
		
		weaponShotgunDmgButton = new JButton("DMG+1");
		weaponShotgunDmgButton.addActionListener(new WeaponButtonListener());
		weaponShotgunButtonPanel.add(weaponShotgunDmgButton);
		weaponShotgunDmgButton.setVisible(false);
		
		weaponShotgunRofButton = new JButton("RoF-1");
		weaponShotgunRofButton.addActionListener(new WeaponButtonListener());
		weaponShotgunButtonPanel.add(weaponShotgunRofButton);
		weaponShotgunRofButton.setVisible(false);
		
		//Weapon Cost Section
		JPanel weaponRegCostPanel = new JPanel();
		weaponRegCostPanel.setLayout(new FlowLayout());
		weaponUpgradePanel.add(weaponRegCostPanel);
		
		weaponRegDmgCostLabel = new JLabel("$" + weaponRegDmgCost);
		weaponRegCostPanel.add(weaponRegDmgCostLabel);
		
		weaponRegDmgField = new JTextField(3);
		weaponRegDmgField.setText("" + weaponRegDmg);
		weaponRegDmgField.setBackground(Color.white);
		weaponRegDmgField.setEditable(false);
		weaponRegCostPanel.add(weaponRegDmgField);
		
		weaponRegRofCostLabel = new JLabel("$" + weaponRegRofCost);
		weaponRegCostPanel.add(weaponRegRofCostLabel);
		
		weaponRegRofField = new JTextField(3);
		weaponRegRofField.setText("" + weaponRegRof);
		weaponRegRofField.setBackground(Color.white);
		weaponRegRofField.setEditable(false);
		weaponRegCostPanel.add(weaponRegRofField);
		
		JPanel weaponPoisonCostPanel = new JPanel();
		weaponPoisonCostPanel.setLayout(new FlowLayout());
		weaponUpgradePanel.add(weaponPoisonCostPanel);
		
		weaponPoisonBuyCostLabel = new JLabel("$" + weaponPoisonCost);
		weaponPoisonCostPanel.add(weaponPoisonBuyCostLabel);
		
		weaponPoisonDmgCostLabel = new JLabel("$" + weaponPoisonDmgCost);
		weaponPoisonCostPanel.add(weaponPoisonDmgCostLabel);
		weaponPoisonDmgCostLabel.setVisible(false);
		
		weaponPoisonDmgField = new JTextField(3);
		weaponPoisonDmgField.setText("N/A");
		weaponPoisonDmgField.setBackground(Color.white);
		weaponPoisonDmgField.setEditable(false);
		weaponPoisonCostPanel.add(weaponPoisonDmgField);
		weaponPoisonDmgField.setVisible(false);
		
		weaponPoisonDurCostLabel = new JLabel("$" + weaponPoisonDurCost);
		weaponPoisonCostPanel.add(weaponPoisonDurCostLabel);
		weaponPoisonDurCostLabel.setVisible(false);
		
		weaponPoisonDurField = new JTextField(3);
		weaponPoisonDurField.setText("N/A");
		weaponPoisonDurField.setBackground(Color.white);
		weaponPoisonDurField.setEditable(false);
		weaponPoisonCostPanel.add(weaponPoisonDurField);
		weaponPoisonDurField.setVisible(false);
		
		JPanel weaponShotgunCostPanel = new JPanel();
		weaponShotgunCostPanel.setLayout(new FlowLayout());
		weaponUpgradePanel.add(weaponShotgunCostPanel);
		
		weaponShotgunBuyCostLabel = new JLabel("$" + weaponShotgunCost);
		weaponShotgunCostPanel.add(weaponShotgunBuyCostLabel);
		
		weaponShotgunDmgCostLabel = new JLabel("$" + weaponShotgunDmgCost);
		weaponShotgunCostPanel.add(weaponShotgunDmgCostLabel);
		weaponShotgunDmgCostLabel.setVisible(false);
		
		weaponShotgunDmgField = new JTextField(3);
		weaponShotgunDmgField.setText("N/A");
		weaponShotgunDmgField.setBackground(Color.white);
		weaponShotgunDmgField.setEditable(false);
		weaponShotgunCostPanel.add(weaponShotgunDmgField);
		weaponShotgunDmgField.setVisible(false);
		
		weaponShotgunRofCostLabel = new JLabel("$" + weaponShotgunRofCost);
		weaponShotgunCostPanel.add(weaponShotgunRofCostLabel);
		weaponShotgunRofCostLabel.setVisible(false);
		
		weaponShotgunRofField = new JTextField(3);
		weaponShotgunRofField.setText("N/A");
		weaponShotgunRofField.setBackground(Color.white);
		weaponShotgunRofField.setEditable(false);
		weaponShotgunCostPanel.add(weaponShotgunRofField);
		weaponShotgunRofField.setVisible(false);
		
		//Tower Section
		//Tower Label Section
		JPanel towerPanel = new JPanel();
		towerPanel.setLayout(new BorderLayout());
		towerPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		contentPanel.add(towerPanel);
		
		JPanel towerLabelPanel = new JPanel();
		towerLabelPanel.setLayout(new FlowLayout());
		towerPanel.add(towerLabelPanel, BorderLayout.NORTH);
		
		JLabel towerLabel = new JLabel("Tower");
		towerLabel.setFont(subHeadFont);
		towerLabelPanel.add(towerLabel);
		
		JPanel towerUpgradePanel = new JPanel();
		towerUpgradePanel.setLayout(new GridLayout(3, 3));
		towerPanel.add(towerUpgradePanel, BorderLayout.CENTER);
		
		JPanel towerRegUpgradeLabelPanel = new JPanel();
		towerRegUpgradeLabelPanel.setLayout(new FlowLayout());
		towerUpgradePanel.add(towerRegUpgradeLabelPanel);
		
		JLabel towerRegLabel = new JLabel("Regular");
		towerRegUpgradeLabelPanel.add(towerRegLabel);
		
		JPanel towerPoisonUpgradeLabelPanel = new JPanel();
		towerPoisonUpgradeLabelPanel.setLayout(new FlowLayout());
		towerUpgradePanel.add(towerPoisonUpgradeLabelPanel);
		
		JLabel towerPoisonLabel = new JLabel("Poison");
		towerPoisonUpgradeLabelPanel.add(towerPoisonLabel);
		
		JPanel towerShotgunUpgradeLabelPanel = new JPanel();
		towerShotgunUpgradeLabelPanel.setLayout(new FlowLayout());
		towerUpgradePanel.add(towerShotgunUpgradeLabelPanel);
		
		JLabel towerShotgunLabel = new JLabel("Shotgun");
		towerShotgunUpgradeLabelPanel.add(towerShotgunLabel);
		
		//Tower Button Section
		JPanel towerRegButtonPanel = new JPanel();
		towerRegButtonPanel.setLayout(new FlowLayout());
		towerUpgradePanel.add(towerRegButtonPanel);
		
		towerRegDmgButton = new JButton("DMG+1");
		towerRegDmgButton.addActionListener(new TowerButtonListener());
		towerRegButtonPanel.add(towerRegDmgButton);
		
		towerRegRofButton = new JButton("RoF-1");
		towerRegRofButton.addActionListener(new TowerButtonListener());
		towerRegButtonPanel.add(towerRegRofButton);
		
		JPanel towerPoisonButtonPanel = new JPanel();
		towerPoisonButtonPanel.setLayout(new FlowLayout());
		towerUpgradePanel.add(towerPoisonButtonPanel);
		
		towerPoisonDmgButton = new JButton("DMG+1");
		towerPoisonDmgButton.addActionListener(new TowerButtonListener());
		towerPoisonButtonPanel.add(towerPoisonDmgButton);
		
		towerPoisonDurButton = new JButton("DUR+1");
		towerPoisonDurButton.addActionListener(new TowerButtonListener());
		towerPoisonButtonPanel.add(towerPoisonDurButton);
		
		JPanel towerShotgunButtonPanel = new JPanel();
		towerShotgunButtonPanel.setLayout(new FlowLayout());
		towerUpgradePanel.add(towerShotgunButtonPanel);
		
		towerShotgunDmgButton = new JButton("DMG+1");
		towerShotgunDmgButton.addActionListener(new TowerButtonListener());
		towerShotgunButtonPanel.add(towerShotgunDmgButton);
		
		towerShotgunRofButton = new JButton("RoF-1");
		towerShotgunRofButton.addActionListener(new TowerButtonListener());
		towerShotgunButtonPanel.add(towerShotgunRofButton);
		
		//Tower Cost Section
		JPanel towerRegCostPanel = new JPanel();
		towerRegCostPanel.setLayout(new FlowLayout());
		towerUpgradePanel.add(towerRegCostPanel);
		
		JLabel towerRegDmgCostLabel = new JLabel("$" + towerRegDmgCost);
		towerRegCostPanel.add(towerRegDmgCostLabel);
		
		towerRegDmgField = new JTextField(3);
		towerRegDmgField.setText("" + towerRegDmg);
		towerRegDmgField.setBackground(Color.white);
		towerRegDmgField.setEditable(false);
		towerRegCostPanel.add(towerRegDmgField);
		
		JLabel towerRegRofCostLabel = new JLabel("$" + towerRegRofCost);
		towerRegCostPanel.add(towerRegRofCostLabel);
		
		towerRegRofField = new JTextField(3);
		towerRegRofField.setText("" + towerRegRof);
		towerRegRofField.setBackground(Color.white);
		towerRegRofField.setEditable(false);
		towerRegCostPanel.add(towerRegRofField);
		
		JPanel towerPoisonCostPanel = new JPanel();
		towerPoisonCostPanel.setLayout(new FlowLayout());
		towerUpgradePanel.add(towerPoisonCostPanel);
		
		JLabel towerPoisonDmgCostLabel = new JLabel("$" + towerPoisonDmgCost);
		towerPoisonCostPanel.add(towerPoisonDmgCostLabel);
		
		towerPoisonDmgField = new JTextField(3);
		towerPoisonDmgField.setText("" + towerPoisonDmg);
		towerPoisonDmgField.setBackground(Color.white);
		towerPoisonDmgField.setEditable(false);
		towerPoisonCostPanel.add(towerPoisonDmgField);
		
		JLabel towerPoisonDurCostLabel = new JLabel("$" + towerPoisonDurCost);
		towerPoisonCostPanel.add(towerPoisonDurCostLabel);
		
		towerPoisonDurField = new JTextField(3);
		towerPoisonDurField.setText("" + towerPoisonDur);
		towerPoisonDurField.setBackground(Color.white);
		towerPoisonDurField.setEditable(false);
		towerPoisonCostPanel.add(towerPoisonDurField);
		
		JPanel towerShotgunCostPanel = new JPanel();
		towerShotgunCostPanel.setLayout(new FlowLayout());
		towerUpgradePanel.add(towerShotgunCostPanel);
		
		JLabel towerShotgunDmgCostLabel = new JLabel("$" + towerShotgunDmgCost);
		towerShotgunCostPanel.add(towerShotgunDmgCostLabel);
		
		towerShotgunDmgField = new JTextField(3);
		towerShotgunDmgField.setText("" + towerShotgunDmg);
		towerShotgunDmgField.setBackground(Color.white);
		towerShotgunDmgField.setEditable(false);
		towerShotgunCostPanel.add(towerShotgunDmgField);
		
		JLabel towerShotgunRofCostLabel = new JLabel("$" + towerShotgunRofCost);
		towerShotgunCostPanel.add(towerShotgunRofCostLabel);
		
		towerShotgunRofField = new JTextField(3);
		towerShotgunRofField.setText("" + towerShotgunRof);
		towerShotgunRofField.setBackground(Color.white);
		towerShotgunRofField.setEditable(false);
		towerShotgunCostPanel.add(towerShotgunRofField);
	}
	
	public void updateShop()
	{
		if (enteredShop)
		{
			playerMoney = Player.getMoney();
			playerHealth = Player.getMaxHealth();
			towerRegDmg = Tower.getDamage();
			towerRegRof = Tower.getRateOfFire();
			towerPoisonDmg = PoisonTower.getDamage();
			towerPoisonDur = PoisonTower.getDamage();
			towerShotgunDmg = ShotgunTower.getDamage();
			towerShotgunRof = ShotgunTower.getRateOfFire();
		}
		moneyField.setText("$" + playerMoney);
		
		if (change)
		{
			undoButton.setEnabled(true);
		} else
		{
			undoButton.setEnabled(false);
		}
		
		updatePlayerSection();
		updateWeaponSection();
		updateTowerSection();
		
		enteredShop = false;
	}
	
	private void updatePlayerSection()
	{
		if (playerMoney >= playerHealthCost)
		{
			healthButton.setEnabled(true);
		} else
		{
			healthButton.setEnabled(false);
		}
		
		healthField.setText("" + playerHealth);
	}
	
	private void updateWeaponSection()
	{		
		//Regular Weapon Section
		if (playerMoney >= weaponRegDmgCost)
		{
			weaponRegDmgButton.setEnabled(true);
		} else
		{
			weaponRegDmgButton.setEnabled(false);
		}
		
		if (playerMoney >= weaponRegRofCost)
		{
			weaponRegRofButton.setEnabled(true);
		} else
		{
			weaponRegRofButton.setEnabled(false);
		}
		
		//Poison Weapon Section
		if (playerMoney >= weaponPoisonCost)
		{
			weaponPoisonBuyButton.setEnabled(true);
		} else
		{
			weaponPoisonBuyButton.setEnabled(false);
		}
		
		if (enteredShop)
		{
			weaponRegDmg = Player.getRegDamage();
			weaponRegRof = Player.getRegRof();
		}
		
		weaponRegDmgField.setText("" + weaponRegDmg);
		weaponRegRofField.setText("" + weaponRegRof);
		
		if (enteredShop)
		{
			weaponPoisonDmg = Player.getPoisonDamage();
			weaponPoisonDur = Player.getPoisonDuration();
		}
		if (boughtPoisonWeapon || Player.hasPoisonWeapon())
		{
			weaponPoisonBuyButton.setVisible(false);
			weaponPoisonBuyCostLabel.setVisible(false);
			weaponPoisonDmgButton.setVisible(true);
			weaponPoisonDmgCostLabel.setVisible(true);
			weaponPoisonDmgField.setVisible(true);
			
			weaponPoisonDmgField.setText("" + weaponPoisonDmg);
			weaponPoisonDurButton.setVisible(true);
			weaponPoisonDurCostLabel.setVisible(true);
			weaponPoisonDurField.setVisible(true);
			weaponPoisonDurField.setText("" + weaponPoisonDur);
		} else
		{
			weaponPoisonBuyButton.setVisible(true);
			weaponPoisonBuyCostLabel.setVisible(true);
			weaponPoisonDmgButton.setVisible(false);
			weaponPoisonDmgCostLabel.setVisible(false);
			weaponPoisonDmgField.setVisible(false);
			weaponPoisonDurButton.setVisible(false);
			weaponPoisonDurCostLabel.setVisible(false);
			weaponPoisonDurField.setVisible(false);
		}
		
		if (playerMoney >= weaponPoisonDmgCost)
		{
			weaponPoisonDmgButton.setEnabled(true);
		} else
		{
			weaponPoisonDmgButton.setEnabled(false);
		}
		
		if (playerMoney >= weaponPoisonDurCost)
		{
			weaponPoisonDurButton.setEnabled(true);
		} else
		{
			weaponPoisonDurButton.setEnabled(false);
		}
		
		//Shotgun Weapon Section
		if (playerMoney >= weaponShotgunCost)
		{
			weaponShotgunBuyButton.setEnabled(true);
		} else
		{
			weaponShotgunBuyButton.setEnabled(false);
		}
		
		if (enteredShop)
		{
			weaponShotgunDmg = Player.getShotgunDamage();
			weaponShotgunRof = Player.getShotgunRof();
		}
		if (Player.hasShotgunWeapon() || boughtShotgunWeapon)
		{
			weaponShotgunBuyButton.setVisible(false);
			weaponShotgunBuyCostLabel.setVisible(false);
			weaponShotgunDmgButton.setVisible(true);
			weaponShotgunDmgCostLabel.setVisible(true);
			weaponShotgunDmgField.setVisible(true);
			
			weaponShotgunDmgField.setText("" + weaponShotgunDmg);
			weaponShotgunRofButton.setVisible(true);
			weaponShotgunRofCostLabel.setVisible(true);
			weaponShotgunRofField.setVisible(true);
			weaponShotgunRofField.setText("" + weaponShotgunRof);
		} else
		{
			weaponShotgunBuyButton.setVisible(true);
			weaponShotgunBuyCostLabel.setVisible(true);
			weaponShotgunDmgButton.setVisible(false);
			weaponShotgunDmgCostLabel.setVisible(false);
			weaponShotgunDmgField.setVisible(false);
			weaponShotgunRofButton.setVisible(false);
			weaponShotgunRofCostLabel.setVisible(false);
			weaponShotgunRofField.setVisible(false);
		}
		
		if (playerMoney >= weaponShotgunDmgCost)
		{
			weaponShotgunDmgButton.setEnabled(true);
		} else
		{
			weaponShotgunDmgButton.setEnabled(false);
		}
		
		if (playerMoney >= weaponShotgunRofCost)
		{
			weaponShotgunRofButton.setEnabled(true);
		} else
		{
			weaponShotgunRofButton.setEnabled(false);
		}
	}
	
	private void updateTowerSection()
	{		
		//Regular tower Section
		if (playerMoney >= towerRegDmgCost)
		{
			towerRegDmgButton.setEnabled(true);
		} else
		{
			towerRegDmgButton.setEnabled(false);
		}
		
		if (playerMoney >= towerRegRofCost)
		{
			towerRegRofButton.setEnabled(true);
		} else
		{
			towerRegRofButton.setEnabled(false);
		}
		
		if (enteredShop)
		{
			towerRegDmg = Tower.getDamage();
			towerRegRof = Tower.getRateOfFire();
		}
		
		towerRegDmgField.setText("" + towerRegDmg);
		towerRegRofField.setText("" + towerRegRof);
		
		//Poison tower Section		
		if (playerMoney >= towerPoisonDmgCost)
		{
			towerPoisonDmgButton.setEnabled(true);
		} else
		{
			towerPoisonDmgButton.setEnabled(false);
		}
		
		if (playerMoney >= towerPoisonDurCost)
		{
			towerPoisonDurButton.setEnabled(true);
		} else
		{
			towerPoisonDurButton.setEnabled(false);
		}
		
		if (enteredShop)
		{
			towerPoisonDmg = PoisonTower.getDamage();
			towerPoisonDur = PoisonTower.getDuration();
		}
		
		towerPoisonDmgField.setText("" + towerPoisonDmg);
		towerPoisonDurField.setText("" + towerPoisonDur);
		
		//Shotgun tower Section		
		if (playerMoney >= towerShotgunDmgCost)
		{
			towerShotgunDmgButton.setEnabled(true);
		} else
		{
			towerShotgunDmgButton.setEnabled(false);
		}
		
		if (playerMoney >= towerShotgunRofCost)
		{
			towerShotgunRofButton.setEnabled(true);
		} else
		{
			towerShotgunRofButton.setEnabled(false);
		}
		
		if (enteredShop)
		{
			towerShotgunDmg = ShotgunTower.getDamage();
			towerShotgunRof = ShotgunTower.getRateOfFire();
		}
		
		towerShotgunDmgField.setText("" + towerShotgunDmg);
		towerShotgunRofField.setText("" + towerShotgunRof);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		if (e.getSource() == backButton)
		{
			Setup.setShop(false);
			Player.setMaxHealth(playerHealth);
			Player.setMoney(playerMoney);
			
			if (Player.hasPoisonWeapon() == false && boughtPoisonWeapon)
			{
				Player.addWeapon(new PoisonWeapon(Player.getPoisonDamage(), Player.getPoisonRof(), Player.getPoisonDuration()));
			}
			if (Player.hasPoisonWeapon())
			{
				Player.setPoisonDamage(weaponPoisonDmg);
				Player.setPoisonDuration(weaponPoisonDur);
			}
			
			if (Player.hasShotgunWeapon() == false && boughtShotgunWeapon)
			{
				Player.addWeapon(new ShotGunWeapon(Player.getShotgunDamage(), Player.getShotgunRof()));
			}
			if (Player.hasShotgunWeapon())
			{
				Player.setShotgunDamage(weaponShotgunDmg);
				Player.setShotgunRof(weaponShotgunRof);
			}
			Player.setRegDamage(weaponRegDmg);
			Player.setRegRof(weaponRegRof);
			


			PoisonTower.setDamage(towerPoisonDmg);
			PoisonTower.setDuration(towerPoisonDur);

			ShotgunTower.setDamage(towerShotgunDmg);
			ShotgunTower.setRateOfFire(towerShotgunRof);
		
			Tower.setDamage(towerRegDmg);
			Tower.setRateOfFire(towerRegRof);
			
			synchronized (TowerList.getTowerArray())
			{
				for (Tower tower : TowerList.getTowerArray())
				{
					tower.updateTower();
				}
			}
			
			enteredShop = true;
			
		} else if (e.getSource() == undoButton)
		{
			change = false;
			enteredShop = true;
			boughtPoisonWeapon = false;
			boughtShotgunWeapon = false;
			updateShop();
		}
	}
	
	private class PlayerButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
			if (e.getSource() == healthButton)
			{
				playerHealth += 10;
				playerMoney -= 100;
			}
			
			change = true;
			updateShop();
		}
		
	}
	
	private class WeaponButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
			if (e.getSource() == weaponRegDmgButton)
			{
				weaponRegDmg++;
				playerMoney -= weaponRegDmgCost;
			} else if (e.getSource() == weaponRegRofButton)
			{
				weaponRegRof--;
				playerMoney -= weaponRegRofCost;
			} else if (e.getSource() == weaponPoisonBuyButton)
			{
				boughtPoisonWeapon = true;
				playerMoney -= weaponPoisonCost;
			} else if (e.getSource() == weaponPoisonDmgButton)
			{
				weaponPoisonDmg++;
				playerMoney -= weaponPoisonDmgCost;
			} else if (e.getSource() == weaponPoisonDurButton)
			{
				weaponPoisonDur++;
				playerMoney -= weaponPoisonDurCost;
			} else if (e.getSource() == weaponShotgunBuyButton)
			{
				boughtShotgunWeapon = true;
				playerMoney -= weaponShotgunCost;
			} else if (e.getSource() == weaponShotgunDmgButton)
			{
				weaponShotgunDmg++;
				playerMoney -= weaponShotgunDmgCost;
			} else if (e.getSource() == weaponShotgunRofButton)
			{
				weaponShotgunRof--;
				playerMoney -= weaponShotgunRofCost;
			}
			
			change = true;
			updateShop();
		}
		
	}
	
	private class TowerButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			
			if (e.getSource() == towerRegDmgButton)
			{
				towerRegDmg++;
				playerMoney -= towerRegDmgCost;
			} else if (e.getSource() == towerRegRofButton)
			{
				towerRegRof--;
				playerMoney -= towerRegRofCost;
			} else if (e.getSource() == towerPoisonDmgButton)
			{
				towerPoisonDmg++;
				playerMoney -= towerPoisonDmgCost;
			} else if (e.getSource() == towerPoisonDurButton)
			{
				towerPoisonDur++;
				playerMoney -= towerPoisonDurCost;
			} else if (e.getSource() == towerShotgunDmgButton)
			{
				towerShotgunDmg++;
				playerMoney -= towerShotgunDmgCost;
			} else if (e.getSource() == towerShotgunRofButton)
			{
				towerShotgunRof--;
				playerMoney -= towerShotgunRofCost;
			}
			
			change = true;
			updateShop();
		}
		
	}
}
